﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using Microsoft.CSharp;
using System.CodeDom.Compiler;

namespace ApplicationGenerator.CommonModule
{
    /// <summary>
    /// Klasa do kompilowania dll'ek
    /// </summary>
    public class ClassCompilerUtility
    {
        /// <summary>
        /// Ścieżka do kompilatora csc.exe
        /// </summary>
        private readonly string _cscPath;

        /// <summary>
        /// Konstruktor
        /// </summary>
        public ClassCompilerUtility()
        {
            _cscPath = Path.Combine(Environment.SystemDirectory.Substring(0, 3), @"WINDOWS\Microsoft.NET\Framework\v3.5\csc.exe");
        }

        /// <summary>
        /// Kompiluje plik
        /// </summary>
        /// <param name="fileToCompile">Plik do kompilacji</param>
        /// <param name="destinationFile">Docelowy plik</param>
        /// <returns>Czy kompilacja przebiegła pomyślnie ?</returns>
        public bool Compile(string fileToCompile, string destinationFile)
        {
            Process process = Process.Start(_cscPath, string.Format("/target:library /out:\"{0}\" \"{1}\"", destinationFile, fileToCompile));
            process.WaitForExit();
            return process.ExitCode == 0;
        }
    }
}
